#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       Translator.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-11-19 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import CollectBase as cb

## bing的翻译接口
#
class TranslatorCall(cb.CollectBase):

	## 方法注释
	#
	#  @param
	#  @return
	#
	def __init__(self, urlDictList, outFunc):
		self.outFunc = outFunc
		cb.CollectBase.__init__(self, urlDictList)
	#------------------------------------------ End def __init__()


	## 方法注释
	#
	#  @param
	#  @return
	#
	def parseData(self, txt, resp, urlDict):
		self.outFunc(txt)
	#------------------------------------------ End def parseData()
#------------------------------------------------------ End class Translator()


## 类注释
#
class TranslatorBing:

	## 方法注释
	#
	#  @param
	#  @return
	#
	def __init__(self, tto="es", tfrom="en", appId="37BD3FF97378CCC15C5BD522BC17D0917949DEBE"):
		self.tto = tto
		self.tfrom = tfrom
		self.appId = appId
	#------------------------------------------ End def __init__()

	## 方法注释
	#
	#  @param
	#  @return
	#
	def translator(self, stringAraays):
		urlDictList = []
		for stringAraay in stringAraays:
			urlDictList.append({"url":"http://api.microsofttranslator.com/v2/Http.svc/TranslateArray?appId=" + self.appId, "method":"POST", "contentType":"text/xml", "data":self.stringXml(stringAraay)})

		#open("log.xml", "w").write(self.stringXml(stringAraay))
		TranslatorCall(urlDictList, self.func)
	#------------------------------------------ End def translator()

	## xml构建
	#
	#  @param
	#  @return
	#
	def stringXml(self, stringAraay):
		xmlHeader = """<?xml version="1.0" encoding="utf-8"?>
<TranslateArrayRequest>
	<AppId>%s</AppId>
	<From>%s</From>
	<Options>
		<Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
		<ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/html</ContentType>
		<ReservedFlags xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
		<State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
		<Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
		<User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
	</Options>
	<Texts>""" % (self.appId, self.tfrom)

		xmlStringTmp = """
		<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">%s</string>"""

		xmlFooter = """
	</Texts>
	<To>%s</To>
</TranslateArrayRequest>""" % self.tto

		xmlString = ""

		for tString in stringAraay:
			xmlString += xmlStringTmp % tString.replace("<", "&lt;").replace(">", "&gt;")

		return xmlHeader + xmlString + xmlFooter
	#------------------------------------------ End def stringXml()

	## 传递过来的方法
	#
	#  @param
	#  @return
	#
	def setOutFunc(self, func):
		self.func = func
	#------------------------------------------ End def setOutFunc()
#------------------------------------------------------ End class TranslatorString()


## 测试 -----------------------------------------
def outTest(txt):
	open("te.xml", "w").write(txt)

if __name__ == "__main__":
	test = TranslatorBing("nl")
	sAraay = [["10000001 Coconut Scented Oil – Bonus Size 120 ml", "30000001 <ul><li>Scents will interact with different parts of the body, balancing, relaxing, providing a better mood and empowering love</li></ul>", "40000001 <div>By using and burning our MYSTIC ROMANCE Premium Home Fragrance Oils you will find a different way to improve your life. These scents, specially selected by us are the ones of highest demand by consumers. Each aroma is naturally associated with particular areas of our brain, where our remembrances come out and remind us of situations, special moments, relations, states of mind or feelings. The brain is an extraordinary part of our body that, in some aspects is still a mystery but its power is recognized because it can handle our balance.</div>"]]

	test.setOutFunc(outTest)
	test.translator(sAraay)
